package com.itheima.d3_annotation;

import org.junit.Test;

import java.lang.reflect.Method;

/*
    目标：掌握注解的解析。

    AnnotatedElement接口提供了解析注解的方法
        public Annotation[] getDeclaredAnnotations()	获取当前对象上面的注解。
        public T getDeclaredAnnotation(Class<T> annotationClass)	获取指定的注解对象
        public boolean isAnnotationPresent(Class<Annotation> annotationClass)	判断当前对象上是否存在某个注解
 */
public class AnnotationTest3 {
    @Test
    public void parseClass(){
        Class<AnnotationTest1> aClass = AnnotationTest1.class;

        boolean present = aClass.isAnnotationPresent(MyTest1.class);
//        System.out.println(present);
        if(present){
            MyTest1 annotation = aClass.getDeclaredAnnotation(MyTest1.class);
            String name = annotation.name();
            System.out.println("name:"+name);
            int age = annotation.age();
            System.out.println("age:"+age);
            int value = annotation.value();
            System.out.println("value:"+value);
        }
    }

    @Test
    public void parseMethod() throws Exception {
        AnnotationTest1 test1 = new AnnotationTest1();
        Class<? extends AnnotationTest1> aClass = test1.getClass();
        Method method = aClass.getMethod("getName");
        System.out.println(method.isAnnotationPresent(MyTest1.class));
        MyTest1 annotation = method.getAnnotation(MyTest1.class);
        System.out.println(annotation.age());
        System.out.println(annotation.value());
        System.out.println(annotation.name());

    }
}
